package com.amigo.online.provider.sync.handle.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.VideoMysqlInfo;
import com.amigo.online.provider.sync.abs.BasicSolrDataProjectProperties;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;
import com.amigo.online.provider.sync.handle.data.solr.service.VideoSolrService;
import com.amigo.online.provider.sync.handle.service.AbstractDataHandler;
import com.amigo.online.provider.sync.handle.service.DataHandler;

@Service("videoSolrDataHandler")
public class VideoSolrDataHandler extends AbstractDataHandler implements DataHandler {

    @Autowired
    SolrTemplate solrTemplate;

    @Autowired
    VideoSolrService videoSolrService;

    @Autowired
    BasicSolrDataProjectProperties basicSolrDataProjectProperties;

    @Autowired
    @Qualifier("solrVideoConvertServiceImpl")
    ConvertJsonObjectService<SolrInputDocument, JSONObject, VideoMysqlInfo> solrVideoconvertJsonObjectService;

    @Override
    public void updateHandle(JSONObject newObject, JSONObject oldObject) {
        int delete = newObject.getIntValue("deleted");
        Long id = newObject.getLong("id");
        long result = 0;
        boolean updateFlag = false;
        if (delete == 0) {
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", id);
            Map<String, String> projectMap = basicSolrDataProjectProperties.getVideoProjectMap();
            Map<String, String> classMap = basicSolrDataProjectProperties.getVideoClassMap();
            for (String temp : oldObject.keySet()) {
                if (basicSolrDataProjectProperties.getVideoUpdateList().contains(temp)) {
                    String solrColumnName = projectMap.get(temp);
                    String solrVideoClassName = classMap.get(temp);
                    if (solrColumnName != null) {
                        temp = solrColumnName;
                    }
					/*SolrInputField field = new SolrInputField(temp);				
					if(solrVideoClassName == null) {
						field.setValue(getMapString(newObject.get(temp)));
					} else {
						try {
							Class<?> clazz = Class.forName(solrVideoClassName);
							field.setValue(getMapString(newObject.getObject(temp, clazz)));
						} catch (ClassNotFoundException e) {
							logger.info("更新获取video中" + temp + "字段类型失败！");
						}				
					}			
					document.put(temp, field);
					logger.info(document.toString());
					updateFlag = true;			*/

                    Map<String, Object> partialUpdate = new HashMap<>(4);
                    if (solrVideoClassName == null) {
                        //field.setValue(getMapString(newObject.get(temp)));
                        partialUpdate.put("set", newObject.get(temp));
                    } else {
                        try {
                            Class<?> clazz = Class.forName(solrVideoClassName);
                            //field.setValue(getMapString(newObject.getObject(temp, clazz)));
                            partialUpdate.put("set", newObject.getObject(temp, clazz));
                        } catch (ClassNotFoundException e) {
                            logger.info("更新获取video中" + temp + "字段类型失败！");
                        }
                    }
                    document.addField(temp, partialUpdate);
                    updateFlag = true;
                }
            }
            if (updateFlag == true) {
                document.addField("_version_", 1);
                logger.info(document.toString());
                result = videoSolrService.updateObject(null, document);
                if (result != 1) {

                }
            }
        } else if (delete == 1) {
            result = videoSolrService.deleteObject(id.toString());
            if (result != 1) {

            }
        }

    }

    @Override
    public void insertHandle(JSONObject newObject) {
        logger.info("插入 video 数据转换前 !");
        SolrInputDocument videoDocument = solrVideoconvertJsonObjectService.convert(newObject);
        logger.info("插入 video  数据转换后!");
        videoSolrService.insertObject(videoDocument);
    }

    @Override
    public void updateInfoByIdThroughHashMap(Long id, Map<String, Object> updateMap) {
        //  Auto-generated method stub

    }

    @Override
    public void insertObject(Object source) {
        SolrInputDocument videoDocument = solrVideoconvertJsonObjectService
                .convertBeanToObject((VideoMysqlInfo) source);
        videoSolrService.insertObject(videoDocument);

    }

}
